using System;
using HslCommunication.Profinet.Siemens;
using WIT.DAQ.Core.Brands.OMRON;
using WIT.DAQ.Core.Enums;

namespace WIT.DAQ.Core.Models;

/// <summary>
/// 设备
/// </summary>
[Serializable]
public class Device
{
	/// <summary>
	/// Id
	/// </summary>
	public Guid Id { get; set; }

	/// <summary>
	/// 名称
	/// </summary>
	public string Name { get; set; }

	/// <summary>
	/// 通讯协议
	/// </summary>
	internal EnumProtocol Protocol { get; set; }

	/// <summary>
	/// IP
	/// </summary>
	internal string IP { get; set; }

	/// <summary>
	/// 端口号
	/// </summary>
	internal int Port { get; set; }

	/// <summary>
	/// 设备类别
	/// </summary>
	internal int Type { get; set; }

	/// <summary>
	/// 设备类别名称
	/// </summary>
	internal string TypeName => ((EnumDeviceType)Type).ToString();

	/// <summary>
	/// 品牌
	/// </summary>
	internal int Brand { get; set; }

	/// <summary>
	/// 品牌名称
	/// </summary>
	internal string BrandName => ((EnumBrand)Brand).ToString();

	/// <summary>
	/// 型号
	/// </summary>
	internal int Model { get; set; }

	/// <summary>
	/// 型号名称
	/// </summary>
	internal string ModelName
	{
		get
		{
			string empty = string.Empty;
			return Type switch
			{
				1 => Brand switch
				{
					1 => ((SiemensPLCS)Model).ToString(), 
					2 => ((PLCModel)Model).ToString(), 
					0 => ((EnumOtherDeviceModel)Model).ToString(), 
					_ => throw new Exception($"获取设备型号时发现未知的设备品牌[{(EnumBrand)Brand}]"), 
				}, 
				0 => ((EnumOtherDeviceModel)Model).ToString(), 
				_ => throw new Exception($"获取型号名称时发现未知的设备类型[{(EnumDeviceType)Type}]"), 
			};
		}
	}

	/// <summary>
	/// 描述
	/// </summary>
	public string Desc { get; set; }

	/// <summary>
	/// 用于控件显示的文本
	/// </summary>
	public string ShowName => "[" + BrandName + "][" + TypeName + "] " + Name;

	/// <summary>
	/// OPCUA地址后缀（包括端口后的"/"符号）
	/// </summary>
	internal string Suffix { get; set; }

	/// <summary>
	/// OPCUA服务器地址（示例：opc.tcp://ip:port/suffix）
	/// </summary>
	internal string Url => $"opc.tcp://{IP}:{Port}{Suffix}";

	/// <summary>
	/// 是否需要使用OPCUA授权账号登录
	/// </summary>
	internal bool? NeedAccount { get; set; }

	/// <summary>
	/// OPCUA授权账号
	/// </summary>
	internal string Account { get; set; }

	/// <summary>
	/// OPCUA授权密码
	/// </summary>
	internal string Pwd { get; set; }

	/// <summary>
	/// 机架号 - SIEMENS PLC （S7_1500以下的型号）
	/// </summary>
	internal int? Rack { get; set; }

	/// <summary>
	/// 插槽号 - SIEMENS PLC （S7_1500以下的型号）
	/// </summary>
	internal int? Slot { get; set; }

	/// <summary>
	/// Modbus数据解析格式（ABCD/BADC/CDAB/DCBA）
	/// </summary>
	internal string DataFormat { get; set; }

	/// <summary>
	/// Modbus起始地址是否从0开始
	/// </summary>
	internal bool? AddressStartWithZero { get; set; }

	/// <summary>
	/// Modbus字符串是否颠倒
	/// </summary>
	internal bool? IsStringReverse { get; set; }

	/// <summary>
	/// Modbus站号
	/// </summary>
	internal int? Station { get; set; }

	/// <summary>
	/// ModbusRtu串口
	/// </summary>
	internal string ComPort { get; set; }

	/// <summary>
	/// ModbusRtu波特率
	/// </summary>
	internal int? BaudRate { get; set; }

	/// <summary>
	/// ModbusRtu数据位
	/// </summary>
	internal int? DataBits { get; set; }

	/// <summary>
	/// ModbusRtu停止位
	/// </summary>
	internal int? StopBits { get; set; }

	/// <summary>
	/// ModbusRtu奇偶校验位
	/// </summary>
	internal int? Parity { get; set; }
}
